class PayTests extends GroovyTestCase {

    def payService

    void testSavePayAndUpdateBalance() {
        try {

            String cardNumber = 'SN00001'
            Membership member = new Membership(name: "会员", discount: 0.8)
            Customer customer = new Customer(name: 'Hu', handPhone: "13774256612", cardNumber: cardNumber)
            member.addToCustomers(customer)
            member.save()

            BigDecimal payAmount = 100.50
            Pay pay = new Pay(actualValue: payAmount, customer: customer, date: new Date())
            payService.savePayAndUpdateBalance(pay)
            Customer customerFromDB = Customer.findByCardNumber(cardNumber)
            assertEquals(payAmount, customerFromDB.balance)

            BigDecimal payAmount2 = 60.49
            Pay pay2 = new Pay(actualValue:payAmount2, customer:customer, date:new Date())
            payService.savePayAndUpdateBalance(pay2)
            customerFromDB = Customer.findByCardNumber(cardNumber)
            assertEquals(payAmount+payAmount2, customerFromDB.balance)
        } catch (Exception e) {
            e.printStackTrace()
            log.error(e)
            throw e
        }


    }
}
